package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.controller.TreeResult;
import com.bjsxt.mapper.AdminRoleMapper;
import com.bjsxt.mapper.MenuMapper;
import com.bjsxt.mapper.RoleMenuMapper;
import com.bjsxt.pojo.AdminRole;
import com.bjsxt.pojo.HostAddResult;
import com.bjsxt.pojo.Menu;
import com.bjsxt.pojo.RoleMenu;
import com.bjsxt.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-10-14
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    //声明用户角色表的mapper
    @Autowired
    private AdminRoleMapper adminRoleMapper;
    //声明角色菜单的mapper
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    //获取菜单表mapper
    @Autowired
    private MenuMapper menuMapper;

    //加载当前用户的菜单信息
    /**
     * @param id 当前要查询的菜单的上级ID
     * @param aid 当前用户的id
     * @return  TreeResult
     */
    @Override
    public List<TreeResult> selMenuInfoService(String id, Integer aid) {
        //获取当前角色的ID
        QueryWrapper<AdminRole> queryWrapper =new QueryWrapper<>();
        //设置查询的内容
        queryWrapper.eq("aid",aid).select("rid");
        //查询
        List<Object> rids = adminRoleMapper.selectObjs(queryWrapper);
        //获取当前角色菜单ID
            //创建条件构造器
        QueryWrapper<RoleMenu> queryWrapper2 =new QueryWrapper<>();
        //设置查询的内容
        queryWrapper2.in("rid",rids).select("mid");
            //查询
        List<Object> mids = roleMenuMapper.selectObjs(queryWrapper2);
        //获取角色的菜单且pid=0
            //创造条件构造器
        QueryWrapper<Menu> queryWrapper3 =new QueryWrapper<>();
        //设置查询的内容
        queryWrapper3.in("mid",mids).eq("pid",id);
        List<Menu> menus = menuMapper.selectList(queryWrapper3);
        //将menus妆花为List<TreeResult>
            //创建List<TreeResult>集合
        List<TreeResult> results = new ArrayList<>();
            //遍历转换
        for (Menu menu:menus){
            TreeResult result = new TreeResult();
            result.setId(menu.getMid());
            result.setText(menu.getMname());
            result.setState("1".equals(menu.getIsparent())?"closed":"open");
            Map<String,Object> map = new HashMap<>();
            map.put("isparent",menu.getIsparent());
            map.put("url",menu.getUrl());
            result.setAttributes(map);
            //将result对象存储到集合中
            results.add(result);
        }
        return results;
    }

    @Override
    public List<TreeResult> selMenuAllInfoService(String id) {
        //获取上级ID为id的菜单信息
            //创造条件构造器
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",id);
        List<Menu> menus = menuMapper.selectList(queryWrapper);
        //将menus妆花为List<TreeResult>
        //创建List<TreeResult>集合
        List<TreeResult> results = new ArrayList<>();
        //遍历转换
        for (Menu menu:menus){
            TreeResult result = new TreeResult();
            result.setId(menu.getMid());
            result.setText(menu.getMname());
            result.setState("1".equals(menu.getIsparent())?"closed":"open");
            Map<String,Object> map = new HashMap<>();
            map.put("isparent",menu.getIsparent());
            map.put("url",menu.getUrl());
            map.put("mdesc",menu.getMdesc());
            map.put("pid",menu.getPid());
            result.setAttributes(map);
            //将result对象存储到集合中
            results.add(result);
        }
        return results;
    }

    @Override
    public HostAddResult insMenuInfoService(Menu menu) {
        //判断当前增加是否选择了父ID
        Integer pid= menu.getPid();
        if(pid==null){
            menu.setIsparent("1");
            menu.setPid(0);
        }else {
            Menu menu1 = new Menu();
            menu1.setMid(pid);
            menu1.setIsparent("1");
            menuMapper.updateById(menu1);
            menu.setPid(pid);
            menu.setIsparent("0");
        }
        //完成新增的菜单的默认数据

        menu.setStatus("0");

        //完成菜单的新增
        int insert = menuMapper.insert(menu);
        //更新上级菜单为父菜单 isparent的值改为1
        return new HostAddResult(insert>0?true:false,"添加成功");
    }

    @Override
    public HostAddResult delMenuInfoService(Menu menu) {
        //获取上级的子菜单数量
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",menu.getPid());
        Integer integer = menuMapper.selectCount(queryWrapper);
        //校验子菜单数量
        if(integer==1){
            Menu menu1 = new Menu();
            menu1.setMid(menu.getPid());
            menu1.setIsparent("0");
            menuMapper.updateById(menu1);
        }
        //删除选择的菜单
        boolean b = menu.deleteById();
        //返回结果
        return new HostAddResult(b,"");
    }

    //加载所有菜单，并按层级关系
    @Override
    public List<TreeResult> selMenuAllInfoService2() {
        //获取所有的菜单信息
        List<Menu> menus = baseMapper.selectList(null);
        //按照层级关系组装菜单
        List<TreeResult> list = getMenuInfo(menus,0);
        //返回结果
        return list;
    }
    //根据id值将所有菜单上中级为pid的菜单全部检索，并转化为TreeResult集合返回
    private List<TreeResult> getMenuInfo(List<Menu> menus, int pid) {
        //创建List<TreeResult>存储符合要求的结果
        ArrayList<TreeResult> results = new ArrayList<>();
        //遍历List<Menu>获取上级ID为pid的menu
        for(Menu m:menus){
            //判断
            if(m.getPid()==pid){
                //将menu转换为TreeResult
                TreeResult treeResult = new TreeResult();
                treeResult.setId(m.getMid());
                treeResult.setText(m.getMname());
                treeResult.setState("1".equals(m.getIsparent())?"closed":"open");
                Map<String,Object> map = new HashMap<>();
                map.put("isparent",m.getIsparent());
                map.put("url",m.getUrl());
                treeResult.setAttributes(map);
                //使用递归
                if("1".equals(m.getIsparent())){
                    List<TreeResult> menuInfo = getMenuInfo(menus, m.getMid());//当前菜单的子菜单集合
                    treeResult.setChildren(menuInfo);//将子菜单存储到父菜单的children的集合
                }
                //将result对象存储到集合中
                results.add(treeResult);
            }
        }
        //返回
        return results;
    }

}
